package org.example.day20250105.flink.jdbc;

import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcExecutionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.example.day20250105.ExcelDataHandle;
import org.example.day20250105.flink.DorisConstant;

/**
 * 通过jdbc写测试流程数据到表中
 * INSERT INTO `test_db_sl`.`ods_xw_test_flow` (`test_info`, `start_step_no`, `cycle_times`, `record_condition`, `voltage_range`) VALUES ('测试信息', '1', '13', '30s/0V/0A', '5v')
 */
public class OdsTestFlowWriteByFlink {
    //todo
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.fromData(ExcelDataHandle.readTestFlowData())
                .addSink(JdbcSink.sink(
                        "INSERT INTO `test_db_sl`.`ods_xw_test_flow` (`test_info`, `start_step_no`, `cycle_times`, `record_condition`, `voltage_range`,`current_range`,`active_substances`," +
                                "`voltage_upper_limit`,`voltage_lower_limit`,`current_upper_limit`,`current_lower_limit`,`start_datetime`,`nominal_capacity`,`battery_batch_number`,`creator`,`remark`,`test_barcode`,`file_name`)" +
                                " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                        (ps,data)->{
                            ps.setString(1,data.getTestInfo());
                            ps.setString(2,data.getStartStepNo());
                            ps.setString(3,data.getCycleTimes());
                            ps.setString(4,data.getRecordCondition());
                            ps.setString(5,data.getVoltageRange());
                            ps.setString(6,data.getCurrentRange());
                            ps.setString(7,data.getActiveSubstances());
                            ps.setString(8,data.getVoltageUpperLimit());
                            ps.setString(9,data.getVoltageLowerLimit());
                            ps.setString(10,data.getCurrentUpperLimit());
                            ps.setString(11,data.getCurrentLowerLimit());
                            ps.setString(12,data.getStartDatetime());
                            ps.setString(13,data.getNominalCapacity());
                            ps.setString(14,data.getBatteryBatchNumber());
                            ps.setString(15,data.getCreator());
                            ps.setString(16,data.getRemark());
                            ps.setString(17,data.getTestBarcode());
                            ps.setString(18,data.getFileName());
                        },
                        new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                                .withUsername(DorisConstant.USERNAME)
                                .withPassword(DorisConstant.PASSWORD)
                                .withDriverName(DorisConstant.DRIVER_CLASS_NAME)
                                .withUrl(DorisConstant.DORIS_JDBC_URL)
                                .build()

                ));
        env.execute();
    }
}
